package com.shj.em.biz.sell.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.sell.entity.Ratio;
import com.shj.em.biz.sell.mapper.RatioMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.plugins.Page;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * 年度回款率目标 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-08-23
 */
@Service
@Transactional
public class RatioServiceImpl extends ServiceImpl<RatioMapper, Ratio> implements RatioService {

    @Autowired
    RatioMapper ratioMapper;

    public Page<Ratio> queryRatioPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        Page<Ratio> page = new Page(pageNo, pageSize);
        List<Ratio> list = ratioMapper.queryRatioPage(page, params);
        return page.setRecords(list);
    }

    public Ratio getRatioByYear(int year){
        Wrapper<Ratio> wrapper = new EntityWrapper();
        wrapper.eq("year", year);
        List<Ratio> list = ratioMapper.selectList(wrapper);
        if(list == null || list.isEmpty()){
            Ratio ratio = new Ratio();
            ratio.setYear(year);
            return ratio;
        }else{
            return list.get(0);
        }
    }
}
